package com.lc.pianostudent.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.R;
import com.lc.pianostudent.adapter.ConversionRecordAdapter;
import com.lc.pianostudent.adapter.HomeNewsAdapter;
import com.lc.pianostudent.adapter.PianoTimeAdapter;
import com.lc.pianostudent.bean.ConversionRecordBean;
import com.lc.pianostudent.bean.HomeNewsBean;
import com.lc.pianostudent.conn.PostGetRecord;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/27.
 */

public class ConversionRecordActivity extends BaseActivity{

    private List<ConversionRecordBean> recordList = new ArrayList<>();
    @BoundView(R.id.conversion_record_recycleview) private XRecyclerView recyclerView;
    private ConversionRecordAdapter conversionRecordAdapter;

    private PostGetRecord.PostGetRecordInfo currentInfo;
    @BoundView(R.id.zhanwei) private ImageView zhanwei;
    @BoundView(R.id.duihuan_record) private TextView duihuan_record;


    private PostGetRecord postGetRecord = new PostGetRecord(new AsyCallBack<PostGetRecord.PostGetRecordInfo>() {

        @Override
        public void onSuccess(String toast, int type, Object o, PostGetRecord.PostGetRecordInfo postGetRecordInfo) throws Exception {
            if(type == 0){
                recordList.clear();
            }
            currentInfo = postGetRecordInfo;

            recordList.addAll(postGetRecordInfo.list);

            if(recordList.size() == 0){
                zhanwei.setVisibility(View.VISIBLE);
                duihuan_record.setVisibility(View.VISIBLE);
            }else {
                zhanwei.setVisibility(View.INVISIBLE);
                duihuan_record.setVisibility(View.INVISIBLE);
                conversionRecordAdapter.notifyDataSetChanged();

            }

//            Log.e("====total",currentInfo.total+"===jy");
        }

        @Override
        public void onEnd(String toast, int type) throws Exception {
            recyclerView.refreshComplete();
            recyclerView.loadMoreComplete();
        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            UtilToast.show(toast);
        }

    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conversion_record);

        setBackTrue();

        initView();
    }

    private void initView(){
        setTitleName("我的兑换记录");


        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        conversionRecordAdapter = new ConversionRecordAdapter(this, recordList);
        recyclerView.setAdapter(conversionRecordAdapter);

        postGetRecord.user_id = BaseApplication.BasePreferences.readUID();
        postGetRecord.page = "1";
        postGetRecord.execute();

        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                postGetRecord.user_id = BaseApplication.BasePreferences.readUID();
                postGetRecord.page = "1";
                postGetRecord.execute(context,false,0);
            }

            @Override
            public void onLoadMore() {
                if(currentInfo.current_page*currentInfo.per_page<currentInfo.total){
                    Log.e("=====page","------11");
                    postGetRecord.user_id = BaseApplication.BasePreferences.readUID();
                    postGetRecord.page = currentInfo.current_page+1+"";
                    postGetRecord.execute(context,false,1);
                }else {
                    Log.e("=====page","------22");
//                    UtilToast.show("暂无更多数据");
                    recyclerView.refreshComplete();
                    recyclerView.loadMoreComplete();
                }
            }
        });

    }
}
